package org.example.one.controller;

import io.lettuce.core.dynamic.annotation.Param;
import org.example.one.common.NewAccount;
import org.example.one.common.Result;
import org.example.one.domain.Account;

import org.example.one.service.AccountService;
import org.example.one.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.example.one.common.Student;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.util.UUID;

@RestController
@RequestMapping("/Student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    AccountService accountService;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @PostMapping("/login")
    public Result login( @RequestBody Account account) {
        if (accountService.getAccount(account)){
            String token= UUID.randomUUID()+"";

            org.example.one.common.Student student=studentService.getstudentByid(account.getSid());
            redisTemplate.opsForValue().set(token,student, Duration.ofDays(7L));

            return Result.succ(200,"登陆成功" ,token);
        }
        return Result.fail(404,"账号或密码错误",account);
    }
    @PostMapping("/addStudent")
    public Result addStudent(@RequestBody Student student , HttpServletRequest request) {
        String token = request.getHeader("token");
        Student student1= (Student) redisTemplate.opsForValue().get(token);
        if(student.getSid()==0||student.getName()==null||student.getClassroom()==null||student.getMajor()==null||student.getCollege()==null){
            return Result.fail(404,"学生信息不完整",null);

        }else if(student1==null){
            return Result.fail(404,"登录信息不全",null);
        }else if(student1.getSid()!=114514){
            return Result.fail(401,"权限不够",null);
        }

        studentService.addStudent(student);
        accountService.addAccount(student.getSid(),"123456");
        return Result.succ(200,"添加成功",null);
    }
    @PutMapping("/putAccountBypassword")
    public Result putAccount(@RequestBody NewAccount newaccount) {
        System.out.println(newaccount);
        if (accountService.getAccount(newaccount.getAccount())){
            accountService.putAccount(newaccount.getAccount().getSid(),newaccount.getNewpassword());
            return Result.succ(200,"修改成功",null);
        }
        return Result.fail(404,"账号或密码不正确",null);
    }
    @PutMapping("/putAccountByadmin/{sid}")
    public Result putAccount(@PathVariable Integer sid,HttpServletRequest request) {
        String token = request.getHeader("token");
        Student student = (Student) redisTemplate.opsForValue().get(token);
        if(student.getSid()!=114514){
            return Result.fail(404,"权限不够",null);
        }
        if (accountService.getById(sid)!=null){
            accountService.putAccount(sid,"123456");
            return Result.succ(200,"修改成功",null);
        }
        return Result.fail(404,"账号未找到",null);
    }
    @GetMapping("/getname")
    public Result getname(@Param("sid") Integer sid) {
        return Result.succ(200,"查找成功",studentService.getstudentByid(sid));
    }
}
